package com.balabala.gym_consume.controller.admin;


import com.balabala.gym_consume.pojo.dto.*;
import com.balabala.gym_consume.pojo.entity.Order;
import com.balabala.gym_consume.pojo.vo.*;
import com.balabala.gym_consume.service.OrderService;
import com.balabala.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;


/**
 * (Order)表控制层
 *
 * @author 何涛
 * @since 2023-05-07 13:42:38
 */
@RestController
@RequestMapping("/admin/order")
@Api(tags = "订单管理接口")
@CrossOrigin
@Slf4j
public class OrderController {


    @Autowired
    private OrderService orderService;

    @PostMapping("/list")
    @ApiOperation(value = "根据条件模糊查询")
    public Result list(@RequestBody  OrderServiceDto orderServiceDto){
        PageHelper.startPage(orderServiceDto.getPage(),orderServiceDto.getLimit());
        List<OrderQueryVo> allByLike = orderService.findAllByLike(orderServiceDto);
        PageInfo<OrderQueryVo> pageInfo = new PageInfo<>(allByLike);
        return Result.ok(pageInfo.getList(),pageInfo.getTotal());
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "根据id删除订单")
    public Result deleteById(@RequestParam Long id){
        boolean b = orderService.removeById(id);
        return Result.ok();
    }


    @PostMapping("/deleteByIds")
    @ApiOperation(value = "根据ids批量删除")
    public Result deleteByIds(@RequestBody BaseDeleteDto baseDeleteDto){
         boolean b = orderService.removeByIds(baseDeleteDto.getIds());
         return Result.ok();
    }
    @GetMapping("/findOrderBySn")
    @ApiOperation(value = "根据订单编号查询出订单详情中的订单和基本信息")
    public Result findOrderBySn(@RequestParam String sn){
         OrderBySnVoAddUserName orderBySn = orderService.findOrderBySn(sn);
         return Result.ok(orderBySn);
    }

    @GetMapping("/findGoodBySn")
    @ApiOperation(value = "根据订单编号查询出订单详情中的商品信息")
    public Result findGoodBySn(@RequestParam String sn){
         GoodSnVo goodBySn = orderService.findGoodBySn(sn);
         return Result.ok(goodBySn);
    }


    @PostMapping("userOrderPageList")
    @ApiOperation("根据用户id查询用户订单")
    public Result getUserOrder(@RequestBody UserOrderPageDTO userOrderPageDTO)
    {
        PageHelper.startPage(userOrderPageDTO.getPage(),userOrderPageDTO.getLimit());
        List<Order> orderList = orderService.list(new LambdaQueryWrapper<Order>().eq(Order::getUserId, userOrderPageDTO.getUserId()));
        PageInfo pageInfo=new PageInfo<Order>(orderList);
        return Result.ok(pageInfo.getList(), pageInfo.getTotal());
    }

    @ApiOperation("修改订单状态")
    @PutMapping("updateOrderStatus")
    public Result updateOrderStatus(UpdateOrderStatusDTO updateOrderStatusDTO)
    {
        log.info("修改的订单编号 sn：{}",updateOrderStatusDTO.getSn());
        return Result.judge(orderService.update(new LambdaUpdateWrapper<Order>().eq(Order::getSn,updateOrderStatusDTO.getSn()).setSql("status="+updateOrderStatusDTO.getStatus())));
    }



    @ApiOperation("批量修改订单状态为已发货")
    @PostMapping("updateOrderStatusByIds")
    public Result updateOrderStatusByIds(@RequestBody UpdateOrderStatusDTO updateOrderStatusDTO)
    {
        log.info("批量修改的订单 ids：{}",updateOrderStatusDTO.getIds());
        boolean b = orderService.updateOrderStatusByIds(updateOrderStatusDTO);
        return Result.ok();
    }

}
